/*
 * @lc app=leetcode.cn id=415 lang=cpp
 *
 * [415] 字符串相加
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    string addStrings(string num1, string num2)
    {
        if (num1.size() < num2.size())
            num1.swap(num2);

        const int N = num1.size() + 10;
        char *ans = new char[N];
        memset(ans, 0, N);

        int i1 = num1.size() - 1;
        int i2 = num2.size() - 1;
        int j = N - 2;
        while (i2 >= 0)
        {
            ans[j] += num2[i2] + num1[i1] - '0' * 2;
            if (ans[j] > 9)
            {
                ans[j - 1] += ans[j] / 10;
                ans[j] %= 10;
            }
            ans[j] += '0';

            i2--;
            i1--;
            j--;
        }

        while (i1 >= 0)
        {
            ans[j] += num1[i1] - '0';
            if (ans[j] > 9)
            {
                ans[j - 1] += ans[j] / 10;
                ans[j] %= 10;
            }
            ans[j] += '0';

            i1--;
            j--;
        }

        int start = 0;
        while(ans[start] == 0) start++;
        j = start;
        while(ans[j] < '0') ans[j] += '0';

        return ans + start;
    }
};
// @lc code=end
